<?php
/**
 * App Module Build (应用模块项目自动构建工具)
 * 
 * 用于自动创建应用模块项目工程
 * 
 * @since 1.0 <2015-8-26> SoChishun <14507247@qq.com> Added.
 */
// 程序版本号
$ver = '1.0';

// 定义应用目录
define('APP_PATH', '../Application/');


if ('build' == I('action')) {
    build();
    exit;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>App Module Build - <?php echo $ver ?></title>
        <style type="text/css">body{font-size:12px;}td span{color:#F00;}td div{color:#CCC;}</style>
    </head>
    <body>
        <form method="post">
            <table>
                <tr><th>厂商代码：</th><td><input type="text" name="vendor_code" placeholder="如：P10000" size="51" /></td></tr>
                <tr><th>模块名称：</th><td><input type="text" name="module_name" placeholder="如：ProductCategory" size="51" /><span>(注意大小写)</span></td></tr>
                <!-- TODO::
                <tr><th>自动创建控制器：</th><td><textarea name="controllers" cols="45" rows="3" placeholder="(可选)多个之间以逗号隔开,如：Product,ProductCategory"></textarea></td></tr>
                <tr><th>自动创建模型：</th><td><textarea name="models" cols="45" rows="3" placeholder="(可选)多个之间以逗号隔开,如:Product,ProductCategory"></textarea></td></tr>
                <tr><th>自动创建视图：</th><td><textarea name="views" cols="45" rows="6" placeholder="(可选)多个之间以逗号隔开;如:Product.product_list(list),Product.product_edit(edit),ProductCategory.category_list(tree_list),ProductCategory.category_edit(tree_edit);系统支持4中默认视图类型：list,edit,tree_list,tree_edit"></textarea></td></tr>
                -->
                <tr><th>&nbsp;</th><td><button type="submit">自动创建项目</button></td></tr>
            </table>
            <input type="hidden" name="action" value="build" />
        </form>
    </body>
</html>
<?php

/**
 * 获取用户传递的参数
 * @param string $name
 * @param string $defv
 * @return string
 * @since 1.0 <2015-8-26> SoChishun Added.
 */
function I($name, $defv = '') {
    if (isset($_POST[$name])) {
        return $_POST[$name];
    }
    if (isset($_GET[$name])) {
        return $_GET[$name];
    }
    return $defv;
}

/**
 * 复制目录
 * @param string $source 源路径,如：../Dir1/Dir2, 注意,不要加后缀/
 * @param string $target 目标路径,如: ../ToDir1/ToDir2, 注意,不要加后缀/
 * @since 1.0 <2015-8-27> SoChishun Added.
 */
function copydir($source, $target, $am_name) {
    if (is_dir($source)) {
        mkdir($target);
        if ($handle = opendir($source)) {
            while (false !== ( $f = readdir($handle) )) {
                if ('.' == $f || '..' == $f) {
                    continue;
                }
                $sourcetmp = $source . '/' . $f;
                $dsttmp = $target . '/' . $f;
                if (is_dir($sourcetmp)) {
                    copydir($sourcetmp, $dsttmp, $am_name);
                } else {
                    copy($sourcetmp, $dsttmp);
                    replace_key($dst, $am_name);
                }
            }
            closedir($handle);
        }
    } else {
        copy($source, $target);
        replace_key($target, $am_name);
    }
}

/**
 * 替换关键词
 * @param string $filename
 * @param string $am_name
 * @since 1.0 <2015-8-27> SoChishun Added.
 */
function replace_key($filename, $am_name) {
    $replaces = array('MSDK' => $am_name,);
    $content = file_get_contents($filename);
    foreach ($replaces as $k => $v) {
        $content = str_ireplace($k, $v, $content);
    }
    file_put_contents($filename, $content);
}

/**
 * 自动创建
 * 
 * @since 1.0 <2015-8-26> SoChishun Added.
 */
function build() {
    $vendor_code = I('vendor_code');
    $module_name = I('module_name');
    if (!$vendor_code || !$module_name) {
        echo '表单数据无效! <a href="?r">返回</a>';
        exit;
    }
    $am_name = 'M' . strtoupper($vendor_code) . $module_name;
    $path1 = APP_PATH . 'MSDK';
    $path2 = APP_PATH . $am_name;
    if (!is_dir($path1)) {
        echo '目录[', $path1, ']不存在,请从官方下载完整SDK包! <a href="?r">返回</a>';
        exit;
    }
    if (is_dir($path2)) {
        echo '目录[', $path2, ']已存在! <a href="?r">返回</a>';
        exit;
    }
    copydir($path1, $path2, $am_name);
    if (is_dir($path2)) {
        echo '项目[', $am_name, ']创建成功! <a href="?r">返回</a>';
    } else {
        echo '项目[', $am_name, ']创建失败,请确认目录权限! <a href="?r">返回</a>';
    }
}
?>